package com.chonglie.tools;

import java.util.ArrayList;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.util.Log;

public class SAXHandler extends DefaultHandler {

	public static final String TAG_START = "startTag";
	public static final String TAG_END = "endTag";
	public static final String TAG_ATTR = "attr";
	public static final String TAG_TEXT = "text";

	private List<String> keys = new ArrayList<String>();
	private List<Object> values = new ArrayList<Object>();

	@Override
	public void startDocument() throws SAXException {
		super.startDocument();
	}

	@Override
	public void endDocument() throws SAXException {
		super.endDocument();
	}

	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		keys.add(TAG_START);
		values.add(localName);
		Log.e(TAG_START, localName);

		String[] value;
		for (int i = 0; i < attributes.getLength(); i++) {
			keys.add(TAG_ATTR);

			value = new String[2];
			value[0] = attributes.getLocalName(i);
			value[1] = attributes.getValue(i);

			values.add(value);

			Log.e(TAG_ATTR, value[0] + ':' + value[1]);
		}
	}

	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		keys.add(TAG_END);
		values.add(localName);
		Log.e(TAG_END, localName);
	}

	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		String value = new String(ch, start, length);
		value = value.trim();
		if (value.length() == 0) {
			return;
		}

		keys.add(TAG_TEXT);
		values.add(value);
		Log.e(TAG_TEXT, value);
	}

	public List<String> getKeys() {
		return keys;
	}

	public List<Object> getValues() {
		return values;
	}
	
	
}
